package com.pizza.control;

import java.io.IOException;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;
import java.util.Map;

import javax.el.ELContext;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.pizza.abstrait.AbstractAdresse;
import com.pizza.abstrait.AbstractClient;
import com.pizza.abstrait.AbstractCommande;
import com.pizza.database.IPizzaManager;
import com.pizza.database.PizzaManager;
import com.pizza.database.PizzaManagerClient;
import com.pizza.model.Client;
import com.pizza.model.Adresse;
import com.sun.faces.util.MessageFactory;
import com.sun.xml.internal.messaging.saaj.soap.MessageImpl;

/**
 * Class ClientBean
 *
 */
public class ClientBean extends AbstractClient
{
	private String motdepasseconfirm;

	private boolean connected;

	private boolean returnRequired;

	private String previousAction;

	private String previousMethod;

	private String previsousViewId;

	private ArrayList<AbstractCommande> troisDernieresCommandes;

	public ClientBean()
	{
		adresseFacturation = new Adresse();
		adressesLivraison = new ArrayList<AbstractAdresse>();
		connected = false;
	}

	/*
	 * public static ClientBean getCurrentInstance() { if (currentinstance ==
	 * null) return (currentinstance = new ClientBean()); else return
	 * currentinstance; }
	 */

	public boolean isConnected()
	{
		return connected;
	}

	public String getMotdepasseconfirm()
	{
		return motdepasseconfirm;
	}

	public void setMotdepasseconfirm(String motdepasseconfirm)
	{
		this.motdepasseconfirm = motdepasseconfirm;
	}

	public String login()
	{
		FacesContext context = FacesContext.getCurrentInstance();
		FacesMessage message = null;
		Client client = null;
		try
		{
			client = PizzaManagerClient.GetPizzaManager().getClientByMail(mail);
			if (client == null)
			{
				message = MessageFactory.getMessage(context, "input_mail_unknown");
				message.setSeverity(FacesMessage.SEVERITY_ERROR);
				message.setSummary("L'''identifiant " + mail + " est inconnu");
				context.addMessage("form_identification:input_mail", message);
				mail = "";
				return "failure";
			}
		} catch (RemoteException e)
		{
			e.printStackTrace();
		}
		if (motdepasse.equals(client.getMotdepasse()))
		{
			connected = true;
			copyFrom(client);
			if (returnRequired)
				return LoginNavigationManager.RETURN_BACK;
			else
				return "logon";
		} else
		{
			message = MessageFactory.getMessage(context, "input_motdepasse_error");
			message.setSeverity(FacesMessage.SEVERITY_ERROR);
			context.addMessage("form_identification:input_motdepasse", message);
			return "failure";
		}
	}

	public String loginPanier()
	{
		String retour = this.login();
		if (! retour.equals("failure"))
			return "panier";
		else
			return retour;
	}

	public String register() throws IOException
	{
		IPizzaManager pizzaManager = PizzaManagerClient.GetPizzaManager();
		if (isConnected())
		{
			pizzaManager.updateClientDB(this);
			copyFrom(pizzaManager.getClientByMail(getMail()));
			return "";
		} else
		{
			// On test si le client ne s'est pas déjà enregistré
			if (pizzaManager.getClientByMail(mail) != null)
				return "fail";
			if (!motdepasse.equals(motdepasseconfirm))
				return "fail";

			Client client = new Client(nom, prenom, mail, motdepasse, new Date(),
					(Adresse) adresseFacturation);
			pizzaManager.createClientDB(client);
			return login();
		}
	}
	
	public String registerPanier() throws IOException
	{
		String retour = this.register();
		if(! retour.equals("fail")) 
			return "panier";
		else 
			return retour;
	}

	public String logout() throws IOException
	{
		ExternalContext ectx = FacesContext.getCurrentInstance().getExternalContext();
		HttpServletResponse response = (HttpServletResponse) ectx.getResponse();
		HttpSession session = (HttpSession) ectx.getSession(false);
		session.invalidate();
		connected = false;

		return "deconnexion";
	}

	public ArrayList<AbstractCommande> getListCommandes()
	{
		ArrayList<AbstractCommande> liste = new ArrayList<AbstractCommande>();
		try
		{
			liste = PizzaManagerClient.GetPizzaManager().getListCommandesClient(this);
		} catch (RemoteException e)
		{
			System.out.println("Erreur récupération liste commandes du client : " + e);
		}
		return liste;
	}

	public void setPreviousAction(String previousAction)
	{
		this.previousAction = previousAction;
	}

	public void setPreviousMethod(String previousMethod)
	{
		this.previousMethod = previousMethod;
	}

	public void setPreviousViewId(String viewId)
	{
		this.previsousViewId = viewId;
	}

	public void setReturnRequired(boolean b)
	{
		this.returnRequired = b;
	}

	public String getPreviousAction()
	{
		return previousAction;
	}

	public String getPreviousMethod()
	{
		return previousMethod;
	}

	public String getPreviousViewId()
	{
		return previsousViewId;
	}

	public ArrayList<AbstractCommande> getTroisDernieresCommandes()
	{
		return getListCommandes();
	}

	public void setTroisDernieresCommandes(
			ArrayList<AbstractCommande> troisDernieresCommandes)
	{
		this.troisDernieresCommandes = troisDernieresCommandes;
	}

	public int getNbColumnsLivraison()
	{
		int nbAdressesLivraisons = adressesLivraison.size();
		return (nbAdressesLivraisons < 4) ? nbAdressesLivraisons : 4;
	}

	public int getNbRowsLivraison()
	{
		return 1;
	}
}
